package com.kingtone.rongji.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kingtone.rongji.ProductDetailActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.Product;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.CartUtils;
import com.kingtone.rongji.tool.RongJiUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CartProductAdapter extends BaseAdapter {

	private LayoutInflater mInflater = null;

	private List<Product> mProducts = null;

	private PriceChanged mPriceChanged;

	private DisplayImageOptions mOptions;

	public interface PriceChanged {
		public void onPriceChanged(String pid);
	}

	public CartProductAdapter(Context context, List<Product> products) {
		mProducts = products;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mOptions = new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565)
				.delayBeforeLoading(100).build();
	}

	public void setPriceChangedListener(PriceChanged listener) {
		this.mPriceChanged = listener;
	}

	public List<Product> getDatas() {
		return mProducts;
	}

	public int getCount() {
		return mProducts == null ? 0 : mProducts.size();
	}

	public Object getItem(int position) {
		return mProducts == null ? null : mProducts.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.cart_list_item, parent,
					false);
		}
		final Product product = (Product) getItem(position);
		if (product == null) {
			return convertView;
		}

		ImageView productImg = (ImageView) convertView
				.findViewById(R.id.id_img_product);
		TextView title = (TextView) convertView.findViewById(R.id.id_tv_title);
		TextView price = (TextView) convertView.findViewById(R.id.id_tv_price);
		ImageView removeImg = (ImageView) convertView
				.findViewById(R.id.id_img_remove);
		EditText num = (EditText) convertView.findViewById(R.id.id_et_nums);
		ImageView minus = (ImageView) convertView
				.findViewById(R.id.id_img_minus);
		ImageView plus = (ImageView) convertView.findViewById(R.id.id_plus);
		TextView priceAll = (TextView) convertView
				.findViewById(R.id.id_tv_price_all);

		title.setText(product.getTitle());
		price.setText("单价：" + RongJiUtils.formatCost(product.getUnitPrice()));
		num.setText(product.getQuantity() + "");
		priceAll.setText(RongJiUtils.formatCost(product.getQuantity()
				* product.getUnitPrice()));

		if (!TextUtils.isEmpty(product.getImageName())) {
			ImageLoader.getInstance().displayImage(
					AppRestClient.convertImageUrl(product.getProductId(),
							product.getImageName()), productImg, mOptions);
		}
		// AppRestClient.convertImageUrl(imageSet.getProductId(), imageName);

		/*
		 * ImageView imageView = (ImageView)
		 * convertView.findViewById(R.id.app_icon); TextView text = (TextView)
		 * convertView.findViewById(R.id.app_name);
		 * imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		 * imageView.setImageResource(product.getIconId());
		 * text.setText(product.getName());
		 */

		minus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Product prd = getProductByPid(product.getProductId());
				if (null != prd) {
					if (prd.getQuantity() > 0) {
						int num = prd.getQuantity() - 1;
						prd.setQuantity(num);
						notifyDataSetChanged();

						CartUtils.deleteFromCart(v.getContext(), prd);

						if (null != mPriceChanged) {
							mPriceChanged.onPriceChanged(prd.getProductId());
						}
					}
				}
			}
		});

		plus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Product prd = getProductByPid(product.getProductId());
				if (null != prd) {
					if (prd.getQuantity() >= 0) {
						int num = prd.getQuantity() + 1;
						prd.setQuantity(num);
						notifyDataSetChanged();

						CartUtils.add2Cart(v.getContext(), prd);

						if (null != mPriceChanged) {
							mPriceChanged.onPriceChanged(prd.getProductId());
						}
					}
				}
			}
		});

		removeImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				removeProductByPid(product.getProductId());
				notifyDataSetChanged();

				CartUtils.deleteProduct(v.getContext(), product);

				if (null != mPriceChanged) {
					mPriceChanged.onPriceChanged(product.getProductId());
				}
			}
		});

		convertView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(),
						ProductDetailActivity.class);
				Bundle ext = new Bundle();
				ext.putString("productId", product.getProductId());
				intent.putExtras(ext);
				view.getContext().startActivity(intent);
			}

		});
		return convertView;
	}

	private Product getProductByPid(String pid) {
		for (Product item : mProducts) {
			if (item.getProductId().equals(pid)) {
				return item;
			}
		}
		return null;
	}

	private void removeProductByPid(String pid) {
		for (Product item : mProducts) {
			if (item.getProductId().equals(pid)) {
				mProducts.remove(item);
				return;
			}
		}
	}
}
